package com.example.demo.database.migration;

import com.example.demo.database.services.base.Migrator;
import com.example.demo.database.services.base.SqlCompiler;

public class InitMigrator extends Migrator {

    @Override
    public void up(SqlCompiler sqlCompiler) {
        sqlCompiler.create("admins", columnGenerator -> {
            columnGenerator.id();
            columnGenerator.string("number").nullable().comment("学生：学号，教师：工号");
            columnGenerator.string("name").nullable().comment("姓名");
            columnGenerator.timestamps();
            columnGenerator.softDeletes();
        });
        sqlCompiler.create("roles", columnGenerator -> {
            columnGenerator.id();
            columnGenerator.string("name").nullable().comment("角色名");
            columnGenerator.string("description").nullable().comment("角色描述");
            columnGenerator.timestamps();
            columnGenerator.softDeletes();
        });
        sqlCompiler.create("permissions", columnGenerator -> {
            columnGenerator.id();
            columnGenerator.string("name").nullable().comment("权限名");
            columnGenerator.string("description").nullable().comment("权限描述");
            columnGenerator.string("data").nullable().comment("权限标识");
            columnGenerator.timestamps();
            columnGenerator.softDeletes();
        });
        sqlCompiler.create("admin_role", columnGenerator -> {
            columnGenerator.id();
            columnGenerator.string("role_id").nullable();
            columnGenerator.string("permission_id").nullable();
            columnGenerator.timestamps();
            columnGenerator.softDeletes();
        });
        sqlCompiler.create("role_permission", columnGenerator -> {
            columnGenerator.id();
            columnGenerator.string("admin_id").nullable();
            columnGenerator.string("role_id").nullable();
            columnGenerator.timestamps();
            columnGenerator.softDeletes();
        });
    }

    @Override
    public void down(SqlCompiler sqlCompiler) {
        sqlCompiler.dropIfExists("admins");
        sqlCompiler.dropIfExists("roles");
        sqlCompiler.dropIfExists("permissions");
        sqlCompiler.dropIfExists("admin_roles");
        sqlCompiler.dropIfExists("role_permissions");
    }
}
